package com.itheima.edu.info.manager.thread;

import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.service.StudentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class StudentSocketRunnable implements Runnable {
    private Socket socket;

    public StudentSocketRunnable(Socket socket) {
        this.socket = socket;
    }

    private static final Logger LOGGER = LoggerFactory.getLogger(StudentSocketRunnable.class);
    StudentService service = new StudentService();

    @Override
    public void run() {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String work = br.readLine();
            String[] split = work.split(",");
            LOGGER.debug("接受学生客户端数据" + Arrays.toString(split));
            switch (split[0]) {
                case "[1]":
                    addStudent(split);
                    break;
                case "[2]":
                    deleteStudent(split[1]);
                    break;
                case "[3]":
                    updateStudent(split);
                    System.out.println(Arrays.toString(split));
                    break;
                case "[4]":
                    findAllStudent();
                    break;
                case "[5]":
                    ieExists(split[1]);
                    break;
                default:
                    System.out.println("输入错误!!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void updateStudent(String[] split) throws IOException {
        Student student = new Student(split[1], split[2], split[3], split[4]);
        service.updateStudent(split[1], student);

        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        bw.write("修改成功");
        bw.flush();
        socket.close();
    }

    public void deleteStudent(String delId) throws IOException {
        service.deleteStudent(delId);
        System.out.println(delId);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        bw.write("删除成功!");
        bw.flush();
        socket.shutdownOutput();
        socket.close();
    }

    private void addStudent(String[] split) throws IOException {
        Student student = new Student(split[1], split[2], split[3], split[4]);
        boolean b = service.addStudent(student);

        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        bw.write(b + "");
        bw.flush();
        socket.shutdownOutput();
        socket.close();
    }

    private void ieExists(String s) throws IOException {
        boolean exists = service.isExists(s);
        //将结果写会客户端
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        bw.write(exists + "");
        bw.flush();
        socket.close();
    }

    private void findAllStudent() throws IOException {
        Student[] allStudent = service.findAllStudent();
        ArrayList<Student> list = new ArrayList<>();
        Collections.addAll(list, allStudent);

        ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
        oos.writeObject(list);
        socket.close();
    }
}
